﻿@model CubicFWNet.Data.Models.PurchaseCompose
@*@model IEnumerable<ProductTechnologyView>*@
@{
    //ViewBag.Title = "价格核算";
    var operationName = ViewBag.OperationName;
    var operationTime = ViewBag.OperationTime;
    var pro = ViewBag.Product as CubicFWNet.Data.Models.Product;
    var customerList = ViewBag.CustomerList as IEnumerable<CubicFWNet.Data.Models.Customer>;
    var proList = ViewBag.ProductList as IEnumerable<CubicFWNet.Data.Models.Product>;
    var pType = ViewBag.Type;
    //var userId = ViewBag.UserId;
    var purchaseNum = ViewBag.PurchaseNum;
    var examine = ViewBag.Examine as CubicFWNet.Data.Models.Examine;
}
<section class="content-header">
    <h1>
        采购单
    </h1>
    <ol class="breadcrumb">
        <li><a href="javascript:void(0);"><i class="fa fa-dashboard"></i>@Resources.Global.PageTitle</a></li>
        <li><a href="@Url.Action("Index", "Home")">首页</a></li>
        <li class="active"><a href="@Url.Action("PurchaseQuery", "Purchase")">采购查询</a></li>
    </ol>
</section>
<section class="content">
    <div class="box box-solid">
        <!-- /.box-header -->
        <div class="box-body">
            <div class="row">
                <form class="form-horizontal" id="frmedit" action="@Url.Action("UpdatePurchase", "Purchase")" method="post">
                    <div class="box-body">
                        <input type="hidden" id="ParentId" name="ParentId" value="@Model.Id" />
                        <input type="hidden" id="pType" name="pType" value="@pType" />
                        <div class="form-group col-md-6">
                            <label class="col-sm-3 control-label">申请人</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="ApplyName" name="ApplyName" value="@operationName" readonly="readonly" />
                            </div>
                        </div>
                        @if (pType == 2)
                        {
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">产品名称</label>
                                <div class="col-sm-9">
                                    <span class="btn btn-primary showProList" style="margin-top: 5px;">选择产品</span>
                                    <span id="listchoisepro">
                                    </span>
                                    @*<input type="text" class="form-control" id="PurchaseName" name="PurchaseName" value="" required/>*@
                                </div>
                            </div>
                            <input type="hidden" id="ProductId" name="ProductId" value="" />
                            <input type="hidden" id="ProductName" name="ProductName" value="" />
                        }
                        @if (pType == 3 && pro != null)
                        {
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">产品名称</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="ProductName" name="ProductName" value="@pro.Name" readonly="readonly" />
                                </div>
                            </div>
                            <input type="hidden" id="ProductId" name="ProductId" value="@pro.Id" />

                        }

                        @*<div class="form-group col-md-6">
                            <label class="col-sm-3 control-label">供应商</label>
                            <div class="col-sm-9">
                                <input type="hidden" name="SupplierId1" id="SupplierId1" />
                                <select class="form-control" type="select" id="SupplierId" name="SupplierId">
                                    <option value="0" selected="selected">==请选择==</option>
                                    @if (customerList != null)
                                    {
                                        foreach (var item in customerList)
                                        {
                                            <option value="@item.Id">@item.Name</option>
                                        }
                                    }
                                </select>
                            </div>
                        </div>*@

                        <div class="form-group col-md-6">
                            <label class="col-sm-3 control-label">供应商</label>
                            <div class="col-sm-9">

                                <input type="hidden" name="SupplierId" id="SupplierId"  />
                                <span class="btn btn-primary" onclick="ShowCusList()">选择供应商</span>&nbsp;&nbsp;&nbsp;&nbsp;
                                <span id="CustomerName"></span>
                            </div>
                        </div>


                        @if (pType == 3 && pro != null)
                        {
                            //var amount = Model.Amount - Model.StandardAmount;
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">采购数量</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control okeydown" id="Amount" name="Amount" value="@Model.Amount" readonly="readonly" />
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">成本价(单价)</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control okeydown" id="CostPrice" name="CostPrice" value="@pro.Price" required />
                                </div>
                            </div>
                        }
                        else
                        {
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">采购数量</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control okeydown" id="Amount" name="Amount" value="" required />
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">成本价(单价)</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control okeydown" id="CostPrice" name="CostPrice" value="" required />
                                </div>
                            </div>
                        }

                        <div class="form-group col-md-6" style="display: none">
                            <label class="col-sm-3 control-label">实际价格</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control okeydown" id="ActualPrice" name="ActualPrice" value="" />
                            </div>
                        </div>
                        <div class="form-group col-md-6">
                            <label class="col-sm-3 control-label">采购单号</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="Number" name="Number" value="@purchaseNum" readonly="readonly" />
                            </div>
                        </div>
                        <div class="form-group col-md-12" style="height: 1px;"></div>
                        <div id="json-content">
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">希望交货时间</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control Timedatepicker" data-des="希望交货时间" id="j_a" name="j_a" value="" required />
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">执行人</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" data-des="执行人" id="j_b" name="j_b" value="" required />
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">订单执行日</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control Timedatepicker" data-des="订单执行日" id="j_c" name="j_c" value="" required />
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">预计到货时间</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control Timedatepicker" data-des="预计到货时间" id="j_d" name="j_d" value="" required />
                                </div>
                            </div>
                            @*<div class="form-group col-md-6">
                                    <label class="col-sm-3 control-label">到货数量</label>
                                    <div class="col-sm-9">
                                        <input type="text" class="form-control" id="ArrivalAmount" data-des="到货数量" name="ArrivalAmount" value="" required />
                                    </div>
                                </div>*@
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">到货日期</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control Timedatepicker" id="ArrivalTime" data-des="到货日期" name="ArrivalTime" value="" />
                                </div>
                            </div>
                            @*<div class="form-group col-md-6">
                                    <label class="col-sm-3 control-label">订单执行完毕</label>
                                    <div class="col-sm-9">
                                        <input type="text" class="form-control" data-des="订单执行完毕" id="j_e" name="j_e" value="" required />
                                    </div>
                                </div>*@
                        </div>
                        <div class="form-group col-md-12" style="height: 1px;"></div>
                        <div class="form-group col-md-6">
                            <label class="col-sm-3 control-label">备注</label>
                            <div class="col-sm-9">
                                <textarea class="form-control" rows="3" placeholder="请输入 ..." name="Memo" id="Memo"></textarea>
                            </div>
                        </div>
                        @if (pType == 3 && examine!=null)
                        {
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">质检备注</label>
                                <div class="col-sm-9">
                                    <textarea class="form-control" rows="3" placeholder="" name="ExamineMemo" id="ExamineMemo" readonly="readonly">@examine.Memo</textarea>
                                </div>
                            </div>
                        }
                        <div class="form-group col-md-12" style="height: 1px;"></div>
                        <div class="form-group col-md-6">
                            <label for="operationName" class="col-sm-3 control-label">操作人</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="operationName" name="operationName" value="@operationName" readonly="readonly">
                            </div>
                        </div>
                        <div class="form-group col-md-6">
                            <label for="operationTime" class="col-sm-3 control-label">日期</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="operationTime" name="operationTime" value="@operationTime" readonly="readonly">
                            </div>
                        </div>
                    </div>
                    <div class="box-footer" style="width: 80%; margin-left: 20px;">
                        <a role="button" href="javascript:void(0)" class="btn btn-primary submitbtn" onclick="formsubmit(1)">提交</a>
                        <span id="historyBack" class="btn btn-primary submitbtn">返回</span>
                        @*<a role="button" href="@Url.Action("PurchaseQuery", "Purchase")" class=" btn btn-primary submitbtn">返回</a>*@
                    </div>
                </form>

            </div>
        </div>
    </div>
</section>
<div id="loadingPage" style="display: none;">
    <div class="loading-shade"></div>
    <div class="loading-content" onclick="$.loading(false)">数据加载中，请稍后…</div>
</div>

    <!--选择供应商-->
<div class="modal" id="cusList" style="display: none">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">选择供应商</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="form-group col-sm-8">
                        <label for="GetCus" class="col-sm-3 ">搜索</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="GetCus" name="GetCus" placeholder="请输入供应商名称..." onkeyup="GetCus()" />
                        </div>
                    </div>
                </div>
                <div id="CusList">
                    @foreach (var item in customerList)
                    {
                        if (item.Id == Model.SupplierId)
                        {
                            <span class="btn btn-primary customerItem" data-cusID="@item.Id" data-cusName="@item.Name" data-sel="1" style="margin:8px">@item.Name</span>
                        }
                        else
                        {
                            <span class="btn btn-default customerItem" data-cusID="@item.Id" data-cusName="@item.Name" data-sel="0" style="margin:8px">@item.Name</span>
                        }
                    }
                </div>
            </div>
            <div class="modal-footer">
                <a role="button" class="btn btn-default pull-left" data-dismiss="modal">关闭</a>
                <a role="button" class="btn btn-primary" data-dismiss="modal">完成</a>
            </div>
        </div>
    </div>
</div>



<!--选择产品-->
@*<div class="modal" id="proList" style="display: none">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    <h4 class="modal-title">选择产品</h4>
                </div>
                <div class="modal-body">
                    <div id="List">
                        @if (proList != null)
                        {
                            foreach (var item in proList)
                            {
                                <span class="btn btn-default productItem" data-proid="@item.Id" data-sel="0" style="margin: 8px">@item.Name</span>
                            }
                        }
                    </div>
                </div>
                <div class="modal-footer">
                    <a role="button" class="btn btn-default pull-left" data-dismiss="modal">关闭</a>
                    <a role="button" class="btn btn-primary" data-dismiss="modal">完成</a>
                </div>
            </div>
        </div>
    </div>*@

<!--选择产品-->
<div class="modal" id="proTableList" style="display: none;">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">选择产品</h4>
            </div>
            <div class="modal-body" style=" height: 450px; overflow: hidden;overflow-y: scroll">
                <div class="col-md-6" style="margin-bottom: 15px;">
                    <div class="input-group">
                        <input id="filter" type="search" aria-controls="footable" class="form-control form-control-static" placeholder="请输入要查询关键字">
                    </div>
                </div>
                <div id="tableList">
                    @if (proList != null)
                    {
                        <table class="footable table table-stripped table-bordered table-hover" id="footable" data-page-size="99999" data-filter=#filter>
                            <thead>
                                <tr>
                                    <th data-sort-ignore="true">
                                        产品名称
                                    </th>
                                    @*<th data-hide="phone,tablet" data-sort-ignore="true">
                                            产品编号
                                        </th>*@
                                    <th data-sort-ignore="true">
                                        选择
                                    </th>
                                </tr>
                            </thead>
                            <tbody class="checkboxs">
                                @if (null != Model)
                                {
                                    int count = 0;
                                    foreach (var item in proList)
                                    {
                                        <tr>
                                            <td>
                                                @Html.DisplayFor(modelItem => item.Name)
                                            </td>
                                            @*<td>
                                                    @Html.DisplayFor(modelItem => item.Number)
                                                </td>*@
                                            <td>
                                                <label>
                                                    @*<input type="checkbox" class="minimal pull-right" date-num="@count" data-proid="@item.Id" data-proname="@item.Name">*@
                                                    <input type="radio" class="minimal pull-right" name="radio" data-proid="@item.Id" data-proname="@item.Name" data-price="@item.Price" data-sizewidth="@item.SizeWidth" data-sizelenth="@item.SizeLenth" data-type="@item.Type" />
                                                </label>
                                            </td>
                                        </tr>
                                        count++;
                                    }
                                }
                            </tbody>
                        </table>
                    }
                </div>
            </div>
            <div class="modal-footer">
                <a role="button" class="btn btn-default pull-left" data-dismiss="modal">关闭</a>
                <a role="button" class="btn btn-primary" data-dismiss="modal">完成</a>
            </div>
        </div>
    </div>
</div>

<style>
    .has-error {
        border-color: #dd4b39;
        box-shadow: none;
    }
</style>


@section Scripts{
    <script type="text/javascript">
        var jsonData = { WebCheckbox: {} };
        jsonData.WebCheckbox.Ids = [];

        $(document).ready(function () {
            $('#footable').footable();

            //日期控件
            $(".Timedatepicker").datepicker({
                autoclose: true,
                language: 'zh', //汉化
                format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
                minView: "month"//选择日期后，不会再跳转去选择时分秒
            });

            $("#frmedit").validate({
                submitHandler: function (form) {
                    var postData = $(form).formSerialize();
                    postData.Data = getParam();
                    postData.Type = $("#pType").val();
                    $.submitForm({
                        url: $(form).attr("action"),
                        param: postData,
                        success: function () {
                            $.modalAlert("操作成功", "success");
                            window.location.href = '@Url.Action("PurQuery", "Purchase")';
                        }
                    });
                }
            });


            $(".showProList").click(function () {
                //$("#proList").modal();
                $("#proTableList").modal();
            });

            //iCheck for checkbox and radio inputs
            $('input[type="checkbox"].minimal,input[type="radio"].minimal').iCheck({
                checkboxClass: 'icheckbox_minimal-blue',
                radioClass: 'iradio_minimal-blue'
            });


            //多选选中
            $('input[type="checkbox"].minimal').on('ifClicked', function (event) {
                var clickNum = $(this).attr("date-num");
                var proId = $(this).attr("data-proId");
                var proName = $(this).attr("data-proName");
                //现在判断时若是选中的，点击完了就变成了未选中了
                if ($(this).is(":checked")) {
                    jsonData.WebCheckbox.Ids.splice($.inArray(proId, jsonData.WebCheckbox.Ids), 1);//删除对应的Id'
                    $(this).removeClass("checkboxitem");
                }
                else//这里判断是没选中，点击完了就变成选中了
                {
                    jsonData.WebCheckbox.Ids.push(proId);//添加对应的Id'
                    $(this).addClass("checkboxitem");
                }

                var phtml = "";
                $(".checkboxs").find('.checkboxitem').each(function (r) {
                    var $this1 = $(this);
                    var _name = $this1.attr('data-proName');
                    phtml += "<span class='btn btn-primary' style='margin-left:5px;margin-top:5px;'>" + _name + "</span>";
                });
                $("#listchoisepro").html(phtml);

                $("#PurchaseNameId").val(jsonData.WebCheckbox.Ids);
                console.log(jsonData.WebCheckbox.Ids);
            });

            //单选
            $('input[type="radio"].minimal').on('ifChecked', function (event) {
                var proId = $(this).attr("data-proId");
                var proName = $(this).attr("data-proName");
                var price = $(this).attr("data-price");
                var proType = $(this).attr("data-type");
                var sizewidth = $(this).attr("data-sizewidth");
                var sizelenth = $(this).attr("data-sizelenth");
                if (proType != "1") {
                    if (isNaN(sizewidth)) {
                        sizewidth = 0;
                    }
                    if (isNaN(sizelenth)) {
                        sizelenth = 0;
                    }
                    price = (sizewidth / 1) * (sizelenth / 1) * (price / 1);
                }

                $(".showProList").text(proName);
                $("#ProductId").val(proId);
                $("#ProductName").val(proName);
                $("#CostPrice").val(price);
                $("#ActualPrice").val(price);
                //alert(proId + ' proId' + proName + ' proName');
            });

        });

        function formsubmit(t) {
            var value = $("#SupplierId").val();
            var proname = $("#PurchaseName").val();
            if (proname == "") {
                $.modalAlert("请选择产品", "warning");
                return;
            }
            if (value == "" || value == "0") {
                $.modalAlert("请选择供应商", "warning");
                return;
            }
            $("#frmedit").submit();
        }

        $(".okeydown").on("keyup", function () {
            var value = $(this).val();
            if (isNaN(value) || value == "" || value == "Infinity" || value == "NaN") {
                $(this).addClass("has-error");
            } else {
                $(this).removeClass("has-error");
            }
        });

        $("#historyBack").click(function () {
            window.history.go(-1);  //返回上一页
            //window.history.back();  //返回上一页
        });

        $(".okeydown").on("keydown", function () {
            var k = event.keyCode;
            if ((k <= 57 && k >= 48) || (k <= 105 && k >= 96) || (k == 8) || (k == 110) || (k == 190)) {
                return true;
            } else {
                return false;
            }
        });

        //json-content
        function getParam() {
            var postdata = [];
            //主键
            $("#json-content").find('input,select,textarea').each(function (r) {
                var $this = $(this);
                var _des = $this.attr('data-des');
                var _id = $this.attr('id');
                var type = $this.attr('type');
                switch (type) {
                    case "checkbox":
                        postdata.push({ des: _des, name: _id, value: $this.is(":checked") });
                        break;
                    case "select":
                        var value = $this.find("option:selected").val() == "" ? null : $this.find("option:selected").val();
                        postdata.push({ des: _des, name: _id, value: value });
                        break;
                    default:
                        var value = $this.val() == "" ? "&nbsp;" : $this.val();
                        postdata.push({ des: _des, name: _id, value: value });
                        break;
                }

            });
            return JSON.stringify(postdata);
        }


        //弹窗显示供应商
        function ShowCusList() {
            $("#cusList").modal();
        }

        //点击选择供应商
        $(".customerItem").click(function () {
            var id = $(this).attr("data-cusID");
            var name = $(this).attr("data-cusName");
            $("#CustomerName").html(name);
            $("#SupplierId").val(id);

            $(".customerItem").addClass("btn-default");
            $(".customerItem").removeClass("btn-primary");
            $(this).addClass("btn-primary");
            $(this).removeClass("btn-default");
        });

        //搜索供应商
        function GetCus() {
            var name = $("#GetCus").val().trim();

            var cusList = $("#CusList .customerItem");
            var num = cusList.size();

            for (var i = 0; i < num; i++) {
                var cusName = $("#CusList .customerItem").eq(i).attr("data-cusName");
                if (cusName.indexOf(name) >= 0) {
                    $("#CusList .customerItem").eq(i).show();
                }
                else {
                    $("#CusList .customerItem").eq(i).hide();
                }
            }
        }
    </script>
}